package com.j2203.service.impl;

import com.j2203.entity.GmUser;
import com.j2203.mapper.UserMapper;
import com.j2203.service.UserService;
import com.j2203.vo.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultMsg findOneById(int uid) {
        ResultMsg resultMsg = new ResultMsg();
        GmUser oneById = userMapper.findOneById(uid);
        if (oneById!=null){
            resultMsg.setData(oneById);
            resultMsg.setStatus(200);
            resultMsg.setMsg("ok");
        }else {
            resultMsg.setData(null);
            resultMsg.setMsg("err");
            resultMsg.setStatus(201);
        }
        return resultMsg;
    }

    @Override
    public ResultMsg findOneByMail(String mail) {
        ResultMsg resultMsg = new ResultMsg();
        System.out.println(mail);
        GmUser oneByMail = userMapper.findOneByMail(mail);

        if (oneByMail!=null){
            resultMsg.setStatus(200);
            resultMsg.setData(oneByMail);
            resultMsg.setMsg("findOne_ok");
        }else{
            resultMsg.setStatus(201);
            resultMsg.setData(null);
            resultMsg.setMsg("findOne_err");
        }
        return resultMsg;
    }

    @Override
    public ResultMsg upDateOne(GmUser gmUser) {
        ResultMsg resultMsg = new ResultMsg();
        if (gmUser.getUid()==null){
            System.out.println(gmUser);
//            查询是否重复
            GmUser oneByMail = userMapper.findOneByMail(gmUser.getEmail());

            if (oneByMail==null){
                int i = userMapper.insertOne(gmUser);
                System.out.println(i+"这是i");
                if (i==1){
                    resultMsg.setMsg("insert_ok");
                    resultMsg.setStatus(200);
                    resultMsg.setData(1);
                }else{
                    resultMsg.setMsg("insert_Err");
                    resultMsg.setStatus(201);
                    resultMsg.setData(0);
                }
            }else {
                resultMsg.setMsg("此邮箱已注册");
                resultMsg.setStatus(202);
                resultMsg.setData(0);
            }

        }else{
            int i = userMapper.updateOne(gmUser);
            if (i==1){
                resultMsg.setMsg("update_ok");
                resultMsg.setStatus(200);
                resultMsg.setData(1);
            }else{
                resultMsg.setMsg("upadte_Err");
                resultMsg.setStatus(201);
                resultMsg.setData(0);
            }
        }

        return resultMsg;
    }

    @Override
    public ResultMsg findOneByMailAndPwd(String email, String password) {
        System.out.println("email = " + email);
        System.out.println("password = " + password);
        ResultMsg resultMsg = new ResultMsg();
        GmUser oneByMailAndPwd = userMapper.findOneByMailAndPwd(email, password);
        System.out.println("oneByMailAndPwd = " + oneByMailAndPwd);
        if (oneByMailAndPwd!=null){
            resultMsg.setData(null);
            resultMsg.setStatus(200);
            resultMsg.setMsg("success");
        }else{
            resultMsg.setData(null);
            resultMsg.setStatus(201);
            resultMsg.setMsg("用户不存在");
        }
        return resultMsg;
    }

}
